/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/

#include "image.h"
#include "imagejpeg.h"
#include "imagepng.h"

Image::Image(const QString& fn)
{
    filename = fn;
    reading = writing = false;
    image_width = image_height = 0;
}

Image::~Image()
{
}

QHash<Image*, QString> image_store;

Image* Image::LoadImage( const QString& file )
{
    Image* new_image = NULL;
    QFileInfo qfi(file);
    QString type = qfi.suffix().toLower();
    if ( (type == "jpg") || (type == "jpeg") )
    {
        new_image = new ImageJpeg(file);
        image_store.insert( new_image, "jpg" );
    }
    else if ( type == "png")
    {
        new_image = new ImagePng(file);
        image_store.insert( new_image, "png" );
    }
    else
    {
        new_image = new Image(file);
        image_store.insert( new_image, "" );
    }
    return new_image;
}

Image* Image::LoadValid(const QString& file )
{
    Image* img = Image::LoadImage( file );
    if ( img->isValid() )
        return img;
    Image::FreeImage( img );
    return NULL;
}

void Image::FreeImage( Image * img )
{
    QString type = image_store.value( img, "NO_IMG" );
    if ( type == "NO_IMG" )
        return;
    else if ( type == "png" )
        delete ((ImagePng*)img);
    else if ( type == "jpg")
        delete ((ImageJpeg*)img);
    else
        delete img;
}

bool Image::getSize(const QString& file, int &w, int &h)
{
    Image* img = Image::LoadValid( file );
    if ( img != NULL )
    {
        w = img->width();
        h = img->height();
        Image::FreeImage( img );
        return true;
    }
    return false;
}

bool Image::startReading( int zoom, int& output_width, int& output_height )
{
    return false;
}

bool Image::startWriting( int width, int height )
{
    return false;
}

int Image::width()
{
    return image_width;
}

int Image::height()
{
    return image_height;
}

uchar* Image::readScanline()
{
    return NULL;
}

bool Image::writeScanline( uchar* row)
{
    return false;
}

bool Image::readImage(QImage *dest_buffer, const QRect& source_rect, const QRect& dest_rect, int zoom )
{
    return false;
}

bool Image::isValid()
{
    return false;
}
